/*
 * 文件名：IndexUtil.java	 
 * 时     间：下午2:17:56
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.zbc.lucene.util;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.cz073586.tools.util.Log4jUtil;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: IndexUtil
 * @author Administrator
 * @date 2016年8月7日 下午2:17:56
 */
public class IndexUtil
{
	private static final Log4jUtil LOG = Log4jUtil.getLog4j(IndexUtil.class);
	
	private static IndexWriter writer;
	
	private static Directory dir;
	
	private static Analyzer analyzer = new IKAnalyzer();;
	
	private IndexUtil()
	{
	}
	
	public static Analyzer getAnalyzer()
	{
		return analyzer;
	}
	
	public static IndexWriter getIndexWriter(String indexPath, boolean isCreate)
	{
		Date start = new Date();
		try
		{
			LOG.info("Indexing to directory '" + indexPath + "'...");
			
			dir = FSDirectory.open(Paths.get(indexPath));
			IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
			
			if (isCreate)
			{
				iwc.setOpenMode(OpenMode.CREATE);
			} else
			{
				iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
			}
			writer = new IndexWriter(dir, iwc);
			
			LOG.info("open IndexWriter" + writer);
			Date end = new Date();
			LOG.info(end.getTime() - start.getTime() + " total milliseconds");
			
		} catch (IOException e)
		{
			LOG.error("open IndexWriter caught a " + e.getClass()
					+ "\n with message: " + e.getMessage());
			closeWriter();
		}
		return writer;
	}
	
	public static void closeWriter()
	{
		if (null != writer)
		{
			try
			{
				writer.close();
				dir.close();
				LOG.info("close IndexWriter" + writer);
			} catch (IOException e)
			{
				LOG.info("close IndexWriter caught a " + e.getClass()
						+ "\n with message: " + e.getMessage());
			}
		}
	}
	
	public static void indexDoc(IndexWriter writer,
			List<Map<FieldEnum, Object>> list)
	{
		//TODO 检查参数为空的异常
		try
		{
			Document doc = new Document();
			
			for (Map<FieldEnum, Object> map : list)
			{
				for (FieldEnum key : map.keySet())
				{
					Object value = map.get(key);
					if (null != key && null != value)
					{
						Field feild = key.newFeild(map.get(key));
						if (null != feild)
							doc.add(feild);
					}
				}
			}
			
			if (writer.getConfig().getOpenMode() == OpenMode.CREATE)
			{
				LOG.info("adding " + list);
				writer.addDocument(doc);
			} else
			{
				LOG.info("updating " + list);
				for (Map<FieldEnum, Object> map : list)
				{
					for (FieldEnum key : map.keySet())
					{
						if (null != key && FieldEnum.STRING_FIELD.equals(key))
						{
							writer.updateDocument(
									new Term(key.name(), map.get(key)
											.toString()), doc);
						}
					}
				}
			}
		} catch (IOException e)
		{
			LOG.error("Lucene index exception : {}", e);
			closeWriter();
		}
	}
}
